import { ref, computed } from 'vue';
import { defineStore } from 'pinia';
import type { AuthState } from '../types';

export const useAuthStore = defineStore('auth', () => {
  const state = ref<AuthState>({
    isAuthenticated: false,
    user: null
  });

  const isAuthenticated = computed(() => state.value.isAuthenticated);
  const currentUser = computed(() => state.value.user);

  function login(email: string, password: string): boolean {
    if (email === 'xuxiao@photo' && password === '123456') {
      state.value.isAuthenticated = true;
      state.value.user = { email, password };
      return true;
    }
    return false;
  }

  function logout() {
    state.value.isAuthenticated = false;
    state.value.user = null;
  }

  return {
    isAuthenticated,
    currentUser,
    login,
    logout
  };
});